package com.xhwl.centiotdata.suppliersystem.eesd.en168;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.xhwl.centiotdata.constant.EesdConstant;
import com.xhwl.common.pojo.cent.device.DeviceProjSupplierCategory;
import lombok.extern.slf4j.Slf4j;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Copyright: Copyright (c) 2021  ALL RIGHTS RESERVED.
 * @Company: 深圳市兴海物联科技有限公司
 * @Author: gyc
 * @CreateDate: 2021/10/4 11:31
 * @UpdateDate: 2021/10/4 11:31
 * @UpdateRemark: init
 * @Version: 1.0
 */
@Slf4j
public class CsgApiUtil {

    /**
     * 获取的e能源管家token
     */
    public static String getEToken(DeviceProjSupplierCategory config) {
        JSONObject properties = config.getProperties();
        String clientId = properties.getString("client_id");
        String clientSecret = properties.getString("client_secret");
        String grantType = properties.getString("grant_type");
        String scopes = properties.getString("scopes");
        String username = properties.getString("username");
        String password = properties.getString("password");
        String ip = properties.getString("ip");
        String token = properties.getString("token");

        String tokenUrl = ip + token;

        Map<String, Object> map = new HashMap<>(16);
        map.put("client_id", clientId);
        map.put("client_secret", clientSecret);
        map.put("grant_type", grantType);
        map.put("scopes", scopes);
        map.put("username", username);
        map.put("password", password);

        String result = null;
        try {
            result = HttpUtil.post(tokenUrl, map, 120000);
        } catch (Exception e) {
            log.error("获取e能管家系统的token失败：{} 失败", e.getMessage());
        }
        String authorization = null;
        String code = JSONObject.parseObject(result).get("code").toString();
        if (code.equals(EesdConstant.SUCCESS_200_STR_CODE)) {
            authorization = "Bearer " + JSONObject.parseObject(JSONObject.parseObject(result).get("data").toString()).get("access_token").toString();
            //log.info("获取e能管家系统的token成功： {}", authorization);
        }
        return authorization;
    }

    /**
     *
     * 调用E能管家系统方法
     * 1、获取电房数据
     * 2、获取设备数据
     * 3、获取监测点数据
     * 4、获取设备实时数据
     * @param token 令牌
     * @param url   请求路径
     * @param param get请求参数 ?key1=value2&key2=value2
     * @param clazz 返回list类型
     * @return
     */
    public static <T> List<T> getEData(String token, String url, Map<String, Object> param, Class<T> clazz) {
        try {
            String body = HttpRequest.get(url).header("Authorization", token).form(param).timeout(1800000).execute().body();
            JSONObject bodyJson = JSONObject.parseObject(body);
            String code = bodyJson.get("code").toString();
            if (code.equals(EesdConstant.SUCCESS_200_STR_CODE)) {
                String data = bodyJson.get("data").toString();
                List<T> list = JSONObject.parseArray(data, clazz);
                return list;
            } else {
                log.error("调用e能管家系统的方法失败");
            }
        } catch (Exception e) {
            log.error("调用e能管家系统的方法：{} 失败", e.getMessage());
        }
       return Collections.emptyList();
    }
}



